﻿using Oliving.Common.SheBei;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChargingSystem4.Common
{
    public static class BllByteHelper
    {
        /// <summary>
        /// 获取程序号的发送数据
        /// </summary>
        /// <returns></returns>
        public static byte[] GetDataNumber()
        {
            // 262	当前焊接规范	当前焊接规范	MODBUS RTU (Zero-based Addressing)	6x	262		16-bit Unsigned		RS485_Data[262]
            byte[] getData = new byte[8]; ; // { 0x01, 0x03, 0x01, 0x06, 0x00, 0x01, 0xFF, 0xFF };

            getData[0] = 0x01;
            getData[1] = 0x03;

            getData[2] = 0x01;
            getData[3] = 0x06;

            getData[4] = 0x00;
            getData[5] = 0x01;

            getData[6] = 0x00;
            getData[7] = 0x00;
            getData = ModbusHelper.AddModbusCRC(getData);

            return getData;
        }


        public static byte[] SetDataNumber(int num = 2)
        {
            //// 01         10          00 00         00 01     02   00 02  FF FE
            //// 设备地址 写寄存器   寄存器起始地址 寄存器个数 字节数 数据   CRC校验

            //byte[] getData = new byte[11];

            //getData[0] = 0x01;
            //getData[1] = 0x10;

            //var byteBegin = ModbusHelper.IntToBytes(262);
            //getData[2] = byteBegin[1];
            //getData[3] = byteBegin[0];

            //getData[4] = 0x00;
            //getData[5] = 0x01;

            //getData[6] = 0x02; // 字节数

            //var byteData = ModbusHelper.IntToBytes(num);
            //getData[7] = byteData[1];
            //getData[8] = byteData[0];

            //getData = ModbusHelper.AddModbusCRC(getData);

            //return getData;

            byte[] paraData = { 0x00, 0x00 };
            var byteBegin = ModbusHelper.IntToBytes(num);
            paraData[0] = byteBegin[1];
            paraData[1] = byteBegin[0];

            return SetData(262, 1, paraData);

        }






        /// <summary>
        ///  读取指定数据
        /// </summary>
        /// <param name="begin"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static byte[] GetData(int begin, int count)
        {
            byte[] getData = new byte[8];

            getData[0] = 0x01;
            getData[1] = 0x03;

            var byteBegin = ModbusHelper.IntToBytes(begin);
            getData[2] = byteBegin[1];
            getData[3] = byteBegin[0];

            var byteCount = ModbusHelper.IntToBytes(count);
            getData[4] = byteCount[1];
            getData[5] = byteCount[0];

            getData[6] = 0x00;
            getData[7] = 0x00;
            getData = ModbusHelper.AddModbusCRC(getData);

            return getData;
        }


        public static byte[] SetData(int begin, int count, byte[] myData)
        {

            // 01         06         00 00          00 02    FF FE
            // 设备地址 写寄存器   寄存器起始地址     数据     CRC校验

            int dataLength = 1 + 1 + 2 + (myData.Length) + 2;

            byte[] getData = new byte[dataLength];

            getData[0] = 0x01;
            getData[1] = 0x06;

            var byteBegin = ModbusHelper.IntToBytes(begin);
            getData[2] = byteBegin[1];
            getData[3] = byteBegin[0];


            getData[4] = myData[0];
            getData[5] = myData[1];
            //for (int i = 0; i < myData.Length; i++)
            //{
            //    getData[7 + i] = myData[i];
            //}



            getData = ModbusHelper.AddModbusCRC(getData);

            return getData;
        }


        public static byte[] SetSysData(int begin, int count, byte[] myData)
        {

            // 01         10         00 00           00 01      02     00 02    FF FE
            // 设备地址 写寄存器   寄存器起始地址  寄存器长度   字节长度      数据     CRC校验

            int dataLength = 1 + 1 + 2 + 2 + 1 + (myData.Length) + 2;

            byte[] getData = new byte[dataLength];

            getData[0] = 0x01;
            getData[1] = 0x10;

            var byteBegin = ModbusHelper.IntToBytes(begin);
            getData[2] = byteBegin[1];
            getData[3] = byteBegin[0];


            var byteCount = ModbusHelper.IntToBytes(count);
            getData[4] = byteCount[1];
            getData[5] = byteCount[0];

            getData[6] = (byte)(count * 2);


            for (int i = 0; i < myData.Length; i++)
            {
                getData[7 + i] = myData[i];
            }



            getData = ModbusHelper.AddModbusCRC(getData);

            return getData;
        }

        public static byte[] SetTimeData()
        {


            #region 授时


            byte[] paraData = {
                            0x00, 0x01, 0x00, 0x02, 0x00, 0x03,
                            0x00, 0x04, 0x00, 0x05, 0x00, 0x07 };

            var byteBegin = ModbusHelper.IntToBytes(DateTime.Now.Year + 1);
            paraData[0] = byteBegin[1];
            paraData[1] = byteBegin[0];

            byteBegin = ModbusHelper.IntToBytes(DateTime.Now.Month);
            paraData[2] = byteBegin[1];
            paraData[3] = byteBegin[0];

            byteBegin = ModbusHelper.IntToBytes(DateTime.Now.Day);
            paraData[4] = byteBegin[1];
            paraData[5] = byteBegin[0];


            byteBegin = ModbusHelper.IntToBytes(DateTime.Now.Hour);
            paraData[6] = byteBegin[1];
            paraData[7] = byteBegin[0];


            byteBegin = ModbusHelper.IntToBytes(DateTime.Now.Minute);
            paraData[8] = byteBegin[1];
            paraData[9] = byteBegin[0];


            byteBegin = ModbusHelper.IntToBytes(DateTime.Now.Second);
            paraData[10] = byteBegin[1];
            paraData[11] = byteBegin[0];

            byte[] getData = BllByteHelper.SetSysData(117, 6, paraData);


            #endregion
            return getData;


        }

        public static byte[] SetF0Data(byte[] myData)
        {

            // 01         F0         00 00           00 00        FF       00 02    FF FE
            // 设备地址 写寄存器   寄存器起始地址    寄存器个数  字节长度     数据     CRC校验

            int dataLength = 1 + 1 + 2 + 2 + 1 + (myData.Length) + 2;

            byte[] getData = new byte[dataLength];

            getData[0] = 0x01;
            getData[1] = 0xF0;

            getData[2] = 0x00;
            getData[3] = 0x00;

            var byteCount = ModbusHelper.IntToBytes(400);
            getData[4] = byteCount[1];
            getData[5] = byteCount[0];

            getData[6] = 0xFF;


            for (int i = 0; i < myData.Length; i++)
            {
                getData[7 + i] = myData[i];
            }


            getData = ModbusHelper.AddModbusCRC(getData);

            return getData;
        }




        public static byte[] SetData(int begin, int count, int val)
        {
            byte[] paraData = { 0x00, 0x00 };
            var byteBegin = ModbusHelper.IntToBytes(val);
            paraData[0] = byteBegin[1];
            paraData[1] = byteBegin[0];

            return SetData(begin, count, paraData);

        }


    }
}
